package yl.hs.bmipfull.controllers.material;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yl.hs.bmipfull.controllers.ControllerBase;
import yl.hs.bmipfull.pojo.material.RawMaterialTransferOrderEntity;
import yl.hs.bmipfull.service.material.IRMTransferService;
import yl.hs.bmipfull.service.system.ISearchViewService;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.PageResult;
import yl.hs.bmipfull.viewmodels.SubmitModel;
import yl.hs.bmipfull.viewmodels.WebResult;

import java.math.BigDecimal;
import java.util.List;

/*
 * RawMaterialTransferController
 * @category
 * @author 谢景广
 * @date 2022-12-23
 */
@Api(value = "原材调拨Web接口", tags = "采购管理——原材调拨操作")
@Slf4j
@RestController
@RequestMapping("/api/rawMaterial/transfer")
public class RawMaterialTransferController extends ControllerBase {

    @ApiOperation(value = "查询调拨计划", notes = "查寻调拨计划")
    @GetMapping("/plan")
    public WebResult queryPlan(){
        try{
            List<RawMaterialTransferOrderEntity> orders = transferService.queryPlan();
            return success("ok",orders);
        }
        catch (Exception ee){
            return error(ee);
        }
    }
    @ApiOperation(value = "查询调拨计划", notes = "查寻调拨计划")
    @RequestMapping(value = {"/all/{key}","/all"},method = RequestMethod.GET)
    public WebResult queryAll(@PathVariable(required = false) String key){
        try{
            List<RawMaterialTransferOrderEntity> orders = transferService.queryAll(key);
            return success("ok",orders);
        }
        catch (Exception ee){
            return error(ee);
        }
    }
    @ApiOperation(value = "保存调拨计划", notes = "保存调拨计划")
    @PostMapping("/plan")
    public WebResult savePlan(@RequestBody SubmitModel model){
        try{
            Integer result = transferService.savePlan(model);
            return success("保存成功",result);
        }
        catch (Exception ee){
            return error(ee);
        }
    }

    @ApiOperation(value = "删除调拨信息", notes = "删除调拨信息")
    @DeleteMapping("/plan/{sn}")
    public WebResult deletePlan(@PathVariable String sn){
        try{
            var result = transferService.findBySn(sn);
            if(result.getState() == 8){
                return fail("不能删除，调拨单已签收，请通过反向调拨处理");
            }
            else if(result.getState() == 1){
                transferService.backAndReturn(sn);
                return success("调拨已作废，出库原材已退回",result);
            }
            else{
                transferService.deleteBySN(sn);
                return success("调拨信息已删除",result);
            }

        }
        catch (Exception ee){
            return error(ee);
        }
    }
    @ApiOperation(value = "签收调拨", notes = "签收调拨")
    @PostMapping("/sign")
    public WebResult saveSign(@RequestBody SubmitModel model){
        try{
            Integer result = transferService.saveSign(model);
            return success("保存成功",result);
        }
        catch (Exception ee){
            return error(ee);
        }
    }

    @ApiOperation(value = "保存皮重", notes = "保存皮重")
    @PutMapping("/tareWeight/{sn}/{weight}")
    public WebResult saveTareWeight(@PathVariable String sn, @PathVariable BigDecimal weight){
        try{
            Integer result = transferService.saveTareWeight(sn,weight);
            return success("保存成功",result);
        }
        catch (Exception ee){
            return error(ee);
        }
    }
    @ApiOperation(value = "保存毛重", notes = "保存毛重")
    @PutMapping("/grossWeight/{sn}/{weight}")
    public WebResult saveGrossWeight(@PathVariable String sn, @PathVariable BigDecimal weight){
        try{
            Integer result = transferService.saveGrossWeight(sn,weight);
            return success("保存成功",result);
        }
        catch (Exception ee){
            return error(ee);
        }
    }

    @ApiOperation(value = "查询调拨记录分页信息", notes = "查询调拨记录分页信息")
    @PostMapping("/record/{rowNum}/{page}")
    public WebResult queryRecord(@RequestBody PageQuery query,
                                 @ApiParam(value = "页码") @PathVariable int page,
                                 @ApiParam(value = "每页条数") @PathVariable int rowNum){
        try{
            PageResult<RawMaterialTransferOrderEntity> result = transferService.queryPage(query, rowNum, page);
            svSVC.updateUserMenuView(query);
            return success("ok",result);
        }
        catch (Exception ee){
            return error(ee);
        }
    }

    private IRMTransferService transferService;

    @Autowired
    public void setTransferService(IRMTransferService transferService) {
        this.transferService = transferService;
    }

    private ISearchViewService svSVC;

    @Autowired
    public void setSvSVC(ISearchViewService svSVC) {
        this.svSVC = svSVC;
    }
}
